package com.centaline.androidsalesblog.rx.database;

import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.model.databaseobject.BrowseDo;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by guilin on 16/6/1.
 * <p/>
 * 描述:获取浏览的ids
 */
public class BrowseIdsOnSubscribe implements Observable.OnSubscribe<ArrayList<String>> {

    private int type;

    public BrowseIdsOnSubscribe(int type) {
        this.type = type;
    }

    @Override
    public void call(Subscriber<? super ArrayList<String>> subscriber) {
        ArrayList<String> ids = new ArrayList<>();
        List<BrowseDo> browseDos = DataSupport
                .where("cityCode = ? and type = ?",
                        AppConstant.CityCode, String.valueOf(type))
                .find(BrowseDo.class);
        if (browseDos != null && browseDos.size() > 0) {
            for (BrowseDo browseDo : browseDos) {
                ids.add(browseDo.getResId());
            }
        }
        if (!subscriber.isUnsubscribed()) {
            subscriber.onNext(ids);
        }
        subscriber.onCompleted();
    }
}
